<template>
  <div>
    <van-cell-group class="item-list">
      <van-cell is-link icon="pending-payment" @click="isState(1)" title="聚合支付-已注册"/>
      <van-cell is-link icon="pending-payment" @click="isState(2)" title="聚合支付-未注册"/>
      <van-cell is-link icon="pending-payment" @click="isState(3)" title="聚合支付-注册失败"/>
      <van-cell is-link icon="pending-payment" @click="isState(6)" title="聚合支付-已注册待审核"/>
      <van-cell is-link icon="pending-payment" @click="isState(4)" title="聚合支付-已注册未签署"/>
      <van-cell is-link icon="pending-payment" @click="isState(5)" title="聚合支付-已注册-蓝海报名失败"/>
      <!--<van-cell is-link icon="gift" title="我收到的礼物" />-->
    </van-cell-group>
  </div>
</template>

<script>
  import Cookies from 'js-cookie';
  import { fetchMerchantCheck, fetchCheck } from '@/api/other';
  import { Cell, CellGroup, Icon, Toast } from 'vant';

  export default {
    components: {
      [Cell.name]: Cell,
      [CellGroup.name]: CellGroup,
      [Toast.name]: Toast,
      [Icon.name]: Icon
    },
    data() {
      return {
        name: '聚合支付'
      };
    },
    created() {
      const merchantHost = this.$route.query.host;
      const merchantToken = this.$route.query.token;
      Toast.loading({
        mask: true,
        message: '效验中...'
      });
      if (merchantHost && merchantToken) {
        // 原生app中传递的两个必传参数
        Cookies.set('merchantHost', merchantHost, { expires: 1 });
        Cookies.set('merchantToken', merchantToken, { expires: 1 });
        // this.checkMerchantStatus();
        this.checkTokenCheck();
      } else {
        Toast('必要参数缺失');
      }
    },
    methods: {
      isState(type) {
        switch (type) {
          case 1:  // 聚合支付-已注册
            this.$router.push('reconciliation');
            break;
          case 2:  // 聚合支付-未注册
            this.$router.push('stateNo');
            break;
          case 3:  // 聚合支付-注册失败
            this.$router.push('registerFail');
            break;
          case 4:  // 聚合支付-已注册未签署
            this.$router.push('registerSuccess');
            break;
          case 5:  // 聚合支付-已注册-蓝海报名失败
            this.$router.push({ path: 'reconciliation', query: { 'blue': false }});
            break;
          case 6:  // 聚合支付-已注册-待审核
            this.$router.push({ path: 'registered' });
            break;
          default:
            this.$router.push('/');
            break;
        }
        // const state = Number(this.$route.query.state);  // 0: 未注册; 1：已注册-已签署；2：已注册-未签署
        // if (state !== null || state !== undefined) {
        //   if (state === 1) {
        //     this.$router.push('stateOpen');
        //   } else if (state === 2) {
        //     this.$router.push({ path: 'registered-no', query: { state: state }})
        //     ;
        //   } else if (state === 0) {
        //     this.$router.push({ path: 'stateNo', query: { state: state }});
        //   } else {
        //     Toast('参数异常~');
        //   }
        // } else {
        //   Toast('无参数！！');
        // }
      },
      checkTokenCheck() {
        fetchCheck()
          .then(response => {
            console.log(response.data);
            if (response.data.success) {
              this.checkMerchantStatus();
              Toast.clear();  // 关闭提示
            }
          })
          .catch(res => {
            Toast('网络异常！');
          });
      },
      checkMerchantStatus() {
        // 效验商户状态
        fetchMerchantCheck()
          .then(response => {
            if (response.data.success) {
              // 1.判断注册状态
              if (response.data.registered) {
                // 已注册
                // 2.判断 富民状态
                if (response.data.fmStatus === 0) {
                  // 待审核
                  this.$router.push({ path: 'registered' });
                } else if (response.data.fmStatus === 1) {
                  // 审核通过
                  // 3.判断 支付方式
                  if (response.data.vxPayStatus && response.data.zfbPayStatus) {
                    // 微信和支付宝都已开启，进入首页
                    // 4.判读 电子协议状态 目前需要人工后台确认
                    if (response.data.agreementSign) {
                      // 后台已确认电子协议
                      this.$router.push({ path: 'reconciliation' });
                    } else {
                      // 金融后台未确认电子协议
                      console.log('金融后台未确认电子协议');
                      this.$router.push('registerSuccess');
                    }
                  } else {
                    // 微信和支付宝存在一个未开启
                    this.$router.push({ path: 'registered' });
                  }
                } else if (response.data.fmStatus === 4) {
                  // 审核成功但未同意协议
                  console.log('用户未确认邮件中电子协议');
                  this.$router.push('registerSuccess');
                } else {
                  // 9:子商户被删除
                  this.$router.push({ path: 'registerFail', query: { 'reason': response.data.fmMsg }});
                }
              } else {
                // 未注册
                this.$router.push('stateNo');
              }
            } else {
              Toast(response.data.message);
            }
          })
          .catch(res => {
            Toast('网络异常！');
          });
      }
    }

  };
</script>

<style>
  body {
    background-color: #f8f8f8;
  }

  .van-cell__title .van-icon {
    font-size: 18px;
  }
</style>
